package com.axxc.jpa.pattendegign.commons;

import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.Random;
import java.util.function.DoubleFunction;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import com.axxc.jpa.pattendegign.commons.TestA.Student;

/**
 * @ClassName TestA
 * @Description //TODO 找出数组中最大值与最小值
 * @author xxc
 * @data 2018年9月10日下午7:54:47
 * @see {@link http://gitee.com/biuabiu/ }
 */
public class TestA {
	public static void main(String[] args) {
		List<Integer> list = Arrays.asList(1654, 07, 11, 22, 225, 34, 354, 135);
		int max = list.get(0), min = list.get(0);
		for (int i = 0; i < list.size(); i++) {
			if (list.get(i) > max) {
				max = list.get(i);
			}
			if (min > list.get(i)) {
				min = list.get(i);
			}
		}
		list.sort((a, b) -> Integer.compare(b, a));
		System.out.println(list.toString());
		list.sort((Integer::compare));// 默认为正序
		System.out.println(list.toString());
		list.stream().filter(i -> i > 50).forEach(System.out::println);
		List<Student> stuLists = Arrays.asList(new Student("A", 16), new Student("F", 18), new Student("B", 20),
				new Student("C", 22), new Student("D", 18));
		stuLists.sort(Comparator.comparing(Student::getAge).reversed().thenComparing(Student::getName));// 逆序,年龄相同,名称再排序

		new Thread(() -> {
			Thread.currentThread().setName("哟哟哟");
			Stream.iterate(0, n -> n + 2 ^ 5)
					.forEach(e -> System.out.println(Thread.currentThread().getName() + "\t" + e));
		}).start();
		stuLists.forEach(System.out::println);
		StudentFuncation<String, Integer, Student> stuFun = Student::new;
		Student apply = stuFun.apply("FF", 245);
		System.out.println(sum(x -> x + 10, 3, 7));// f(x)=x+10;f(x)中(x=3->7)的阴影面积
		stuLists.parallelStream()// 多线程并行计算
				.filter(s -> s.getAge() >= 18)// 筛选年龄大于18
				.sorted(Comparator.comparing(Student::getAge).thenComparing(Student::getName))// 按年龄排序,相等时按名称排序
				.collect(Collectors.groupingBy(Student::getName)).forEach((k, v) -> {
					System.out.println(k);
					System.out.println(v);
				});
	}

	public static double sum(DoubleFunction<Double> f, double a, double b) {
		System.out.println(f.apply(a));
		System.out.println(f.apply(b));
		return f.apply(a) * f.apply(b) * (b - a) / 2;
	}

	public static class Student {

		public Student() {
			super();
			// TODO Auto-generated constructor stub
		}

		public Student(String name, Integer age) {
			super();
			this.name = name;
			this.age = age;
		}

		private String name;
		private Integer age;

		public String getName() {
			return name;
		}

		public void setName(String name) {
			this.name = name;
		}

		public Integer getAge() {
			return age;
		}

		public void setAge(Integer age) {
			this.age = age;
		}

		@Override
		public String toString() {
			return "Student [name=" + name + ", age=" + age + "]";
		}

	}

}
